package com.java.revicer;

import com.java.instans.MqConst;
import com.java.service.SmsService;
import com.java.sms.utils.SmsProperties;
import com.java.vo.SmsDTO;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class SmsReceiver {

    @Autowired
    private SmsService smsService;

    @Autowired
    SmsProperties smsProperties;

    //短信的监听器 QueueBinding 队列绑定 value --- key --- router
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_MSM_ITEM,durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_MSM),
            key = {MqConst.ROUTING_MSM_ITEM}
    ))
    public void send(SmsDTO smsDTO , Message message, Channel channel){
        Map<String, Object> map = new HashMap<>();
        map.put("code",smsDTO.getMessage());
        //发送短信服务层
        smsService.send(smsDTO.getMobile(),smsProperties.getTemplateCode(),map);
    }
}